
<%@page import="cn.edu.ujn.dao.Customer"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() 
	                   + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet"
	href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script
	src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
	
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- 引入样式文件和动态控制 -->
		<link href="<%=path %>/css/bootstrap.min.css" rel="stylesheet">
        <script src="<%=path %>/js/jquery-3.2.1.min.js"></script>
        <script src="<%=path %>/js/bootstrap.min.js"></script>
      <title>客户管理页面</title>
</head>
 <body>
 
            <nav class="navbar navbar-inverse" role="navigation">
                <div class="container-fluid">
                    <div class="navbar-header">
                        <button type="button" class="navbar-toggle" data-toggle="collapse"
                            data-target="#example-navbar-collapse">
                            <span class="sr-only">切换导航</span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                        </button>
                       
                    </div>
                    <div class="collapse navbar-collapse" id="example-navbar-collapse">
                        
                        <ul class="nav navbar-nav navbar-right">
                    <li><a>欢迎您,${USER_SESSION.userName}</a></li>
					<li><a href="${pageContext.request.contextPath }/logout">安全退出</a></li>
					<li><a href="${pageContext.request.contextPath }/customerdisplay">返回</a></li>
                        </ul>
                    </div>
                </div>
            </nav>


            <div class="container-fluid">
                <div class="row">
                    <div class="col-sm-2">
                        <a href="#" class="list-group-item active"><span class="glyphicon glyphicon-home"></span>客户查询
                        </a>
                        
                    </div>
                    <!--左边菜单栏-->
                    <div class="col-sm-10">
                        <ol class="breadcrumb">
                            <li class="active">菜单
                            </li>
                            <li class="active">客户管理信息
                            </li>
                        </ol>

                        <div class="panel panel-default">
                            <div class="panel-heading">
                                搜索
                            </div>
                            <div class="panel-body">
                                <form action="${pageContext.request.contextPath}/customer/find" method="post"
                                    class="form-inline" role="form">
                                    <div class="form-group">
                                        <input type="text" class="form-control" id="custId" placeholder="客户ID"
                                            name="custId" />
                                    </div>
                                    <div class="form-group">
                                        <input type="text" class="form-control" id="custName" placeholder="客户名称"
                                            name="custName" />
                                    </div>
                                    <div class="form-group">
                                        <input type="text" class="form-control" id="custAddress" placeholder="客户地址"
                                            name="custAddress" />

                                    </div>
                                    <div class="form-group">
                                        <input type="text" class="form-control" id="custIndustry" placeholder="客户工厂"
                                            name="custIndustry" />

                                    </div>
                                    <div class="form-group">
                                        <input type="text" class="form-control" id="custSource" placeholder="客户源"
                                            name="custSource" />
                                    </div>
                                    <button type="submit" class="btn btn-primary">查找</button>
                                </form>
                            </div>
                        </div>
                        
                        <!--
                        列表展示
                    -->
                        <div class="table-responsive">
                            <table class="table table-striped ">
                                <thead>
                                    <tr>
                                        <th>客户ID</th>
                                        <th>客户姓名</th>
                                        <th>客户地址</th>
                                        <th>客户工厂</th>
                                        <th>客户源</th>
                                        <th>操作</th>
                                        <th>操作<th/>
                                    </tr>
                                </thead>
                                <% 
                                ArrayList<Customer> list = (ArrayList<Customer>)request.getAttribute("findCustomerByType");//此处是取出所存储的数据
                                 int page_current = 1; //当前页数
                                 int page_begin = 0;  //起始点,注意:下标从0开始
                                 int page_end = 9;   //终点,每页十条信息
                                 int total_count = 25;
                                 if(list != null)
                                     total_count = list.size();   //信息的总量
                                  int page_total = total_count / 10 + (total_count % 10 != 0 ? 1 : 0);
                                  if(request.getParameter("begin") != null) {
                                      page_current = Integer.parseInt(request.getParameter("begin"));  //获取当前页数
                                                 }
                        
                                 page_begin = (page_current - 1) * 10;
                                 page_end = page_begin + 9 > total_count ? total_count : page_begin + 9;
                                 request.getSession().setAttribute("page_current", page_current);  //保存到session中
                                 request.getSession().setAttribute("page_total", page_total);
                            %>
                        

                            <tbody>
			
                                <c:forEach items="${findCustomerByType}" var="AllCustomer" begin="<%=page_begin %>" end="<%=page_end %>" >
                                    <tr>
                                 <td>${AllCustomer.custId}</td>
                                 <td>${AllCustomer.custName}</td>
                                 <td>${AllCustomer.custAddress}</td>
                                 <td>${AllCustomer.custIndustry}</td>
                                 <td>${AllCustomer.custSource}</td>


                                  <td><a href="#"
                                            class="btn btn-danger" role="button" onclick="deleteCustomer(${AllCustomer.custId})" >删除</a></td>
                                  <td><a href="${pageContext.request.contextPath }/customer/update/${AllCustomer.custId}"
                                            class="btn btn-danger" role="button" onclick="updateCustomer(${AllCustomer.custId})" >更改</a></td>
                                    </tr>
                                    
                                </c:forEach>
                               
                    
                        
                            </tbody>
                             
                            </table>

                            
                        </div>
                        <ul class="pagination" style="float:none;" >
                            <li><a href=>&laquo;</a>
                            </li>
                             <c:if test="${sessionScope.page_current != 1 }">
                        <li><a href="${pageContext.request.contextPath }/customer/find?begin=${sessionScope.page_current - 1 }&custId=${custId}&custName=${custName}&custAddress=${custAddress}&custIndustry=${custIndustry}&custSource=${custSource}">上一页</a></li>
                        </c:if>
                        <li class="active"> <a> ${sessionScope.page_current }</a>
                        <c:if test="${sessionScope.page_current != sessionScope.page_total }">
                        <li><a href="${pageContext.request.contextPath }/customer/find?begin=${sessionScope.page_current + 1 }&custId=${custId}&custName=${custName}&custAddress=${custAddress}&custIndustry=${custIndustry}&custSource=${custSource}">下一页</a></li>
                        </c:if>
                        当前页数 : ${sessionScope.page_current } / ${sessionScope.page_total }
                         
                        </ul>
                    </div>
                </div>
            </div>
   
            <script type="text/javascript">
                function deleteCustomer(id) {
                    if(confirm('确实要删除该客户吗?')) {
                $.post("<%=basePath%>customer/delete",{"id":id},
                function(data){
                            if(data =="OK"){
                                alert("客户删除成功！");
                                window.location.reload();
                            }else{
                                alert("删除客户失败！");
                                window.location.reload();
                            }
                        });
                    }
                }
                </script>
                
                
        </body>

        </html>
